# Installing from a tarball

## Requirements

The target machine runs Linux with

- GRUB
- filesystem utilities: mke2fs, blkid
- archiving utilities: tar, gzip

## Installation to existing partitions

**ALL DATA ON THE TARGET ROOT PARTITION WILL BE ERASED**

 1. Check if the target root partition exists.
 2. Check if the target EFI partition exists.
 3. Check if there is a FAT filesystem on the target EFI partition:
    lsblk -n -o fstype $TARGET_ESP
    This command should print `vfat`.
 4. Create an ext4 filesystem on the target root partiton:
    mke2fs -t ext4 -b 4096 -L BoringLinux $TARGET_ROOT
 5. Mount the newly created filesystem:
    mount -t ext4 -o rw,data=ordered,noatime,errors=remount-ro $TARGET_ROOT /mnt
 6. Unpack the tarball into the newly created filesystem:
    tar -x -a -v -f blinux-rootfs-${ARCH}-${VERSION}.tgz -C /mnt
 7. Mount the EFI filesystem. Be careful to NOT make it case-sensitive:
    mount -t vfat -o rw,codepage=866,iocharset=ascii,utf8,noatime $TARGET_ESP /mnt/boot/efi
 8. Copy the state files to the target root filesystem. The set of the state
    files includes, but is not limited to,
    - /etc/machine-id
    - /etc/hostname 
    - /etc/fstab
    - /etc/passwd
    - /etc/shadow
    - /etc/group
    - /etc/gshadow
    - /root/.ssh/authorized_keys
    - /etc/ssh/ssh_host_*_key*
    - /root/state.list
    - /etc/hasher-priv/user.d/*
    cat /root/state.list | ( cd / && cpio -pumd -v /mnt )
 9. Enable network related services:
    a. systemctl enable --root=/mnt systemd-networkd.service
    b. systemctl enable --root=/mnt systemd-resolved.service
10. Figure out the UUID of the target root filesystem:
    new_uuid=`lsblk -n -o UUID $TARGET_ROOT`
11. Adjust /mnt/etc/fstab -- insert the target root filesystem UUID ($new_uuid)
12. Adjust /mnt/boot/grub/grub.cfg:
    a. Change the default entry to non-volatile
    b. Adjust `set rootfs_uuid=` directive according to the new_uuid
    c. If using main+backup root partitions: add an entry to boot
        the previous version of the system.
13. Re-install the grub boot loader, so it will look for modules and
    configuration in the newly populated target root filesystem.
    a. If UEFI support BootNNNN variables properly: 
       grub-install --boot-directory=/mnt/boot --efi-directory=/mnt/boot/efi --bootloader-id=boringlinux
    b. Otherwise fall back to a "removable" layout:
       grub-install --boot-directory=/mnt/boot --efi-directory=/mnt/boot/efi --no-nvram --removable
14. Unmount target filesystems:
    a. umount /mnt/boot/efi
    b. umount /mnt
15. Reboot
